package com.example.demo.service.impl;

import com.example.demo.dao.DishesDao;
import com.example.demo.model.Dishes;
import com.example.demo.model.Ingredients;
import com.example.demo.service.DishesService;
import com.example.demo.util.RecognizeUtil;
import com.example.demo.util.hanLP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DishesServiceImpl implements DishesService {
    @Autowired(required = false)
    private RecognizeUtil reg;
    @Autowired
    private hanLP hlp;
    @Autowired
    private DishesDao dishesDao;


    @Override
    public List<Dishes> getAllDishes() {
        return dishesDao.findAll();
    }

    @Override
    public Dishes getDishesById(Long dishId) {
        return dishesDao.selectByPrimaryKey(dishId);
    }


    @Override
    public Dishes recognizeFood(String imageBase64)
    {
        Dishes dish = reg.Recognize(imageBase64);
        // 判断菜品是否已经在数据库中
        Dishes existingDish = dishesDao.findByName(dish.getName());
        if (existingDish == null) {
            // 如果菜品不在数据库中，则插入数据库
            dishesDao.insert(dish);
        }
        return dish;
    }


    public void incrementDishViewCount(Long dishId) {
        dishesDao.incrementDishViewCount(dishId);
    }

    @Override
    public void add(Dishes dishes) {

        // 判断菜品是否已经在数据库中
        Dishes existingDish = dishesDao.findByName(dishes.getName());
        if (existingDish == null) {
            // 如果菜品不在数据库中，则插入数据库
            dishesDao.insert(dishes);
        }
    }

    @Override
    // 根据食材名称获取菜品列表
    public List<Dishes> getDishesByIngredientName(String ingredientName) {
        List<Dishes> dishes = new ArrayList<>();
        List<Dishes> allDishes = dishesDao.findAll();

        for (Dishes dish : allDishes) {
            if(dish.getDIngredients()!=null)
            {
                for (Ingredients ingredient : dish.getDIngredients()) {
                    if (ingredient.getName().equals(ingredientName)) {
                        dishes.add(dish);
                        break;
                    }
                }
            }
        }

        return dishes;
    }

    @Override
    // 根据食材名称对菜品进行分类
    public Map<String, List<Dishes>> classifyDishesByIngredient() {
        List<Dishes> dishes = dishesDao.findAll();
        Map<String, List<Dishes>> classifiedDishes = new HashMap<>();

        for (Dishes dish : dishes) {
            List<Ingredients> ingredients = dish.getDIngredients();
            if(dish.getDIngredients()!=null){
                for (Ingredients ingredient : ingredients) {
                    String ingredientName = ingredient.getName();
                    List<Dishes> classifiedList = classifiedDishes.computeIfAbsent(ingredientName, k -> new ArrayList<>());
                    classifiedList.add(dish);
                }
            }
        }

        return classifiedDishes;
    }
    @Override
    public List<Dishes> RecommandForUserInfo(String description){
        List<Dishes> dishes = hlp.Search(description,dishesDao);
        return dishes;
    }

}
